// pages/genjin/genjin.js

import { promisify } from '../../utils/promise.util'
import { $init, $digest } from '../../utils/common.util'
var dateTimePicker = require('../../utils/dateTimePicker.js')

const app=getApp()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    content: "",
    images: [],
    upload:[],
    id:"",
    select: -1,
    selectData:"",
    status:true,
    date: '2018-10-01',
    time: '12:00',
    dateTimeArray: null,
    dateTime: null,
    dateTimeArray1: null,
    dateTime1: null,
    startYear: 2000,
    endYear: 2050
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id,
      status: options.status == 'To_Be_Follow_up' ?  false : true
    })
    console.log(options.status);

    // 获取完整的年月日 时分秒，以及默认显示的数组
    var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    // 精确到分的处理，将数组的秒去掉
    var lastArray = obj1.dateTimeArray.pop();
    var lastTime = obj1.dateTime.pop();

    this.setData({
      dateTime: obj.dateTime,
      dateTimeArray: obj.dateTimeArray,
      dateTimeArray1: obj1.dateTimeArray,
      dateTime1: obj1.dateTime
    });

    $init(this)

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  uploadImg: function () {
    var that = this;
    wx.uploadFile({
      url: app.globalData.httpurl +'upload',
      filePath: this.data.images[0],
      name: 'images',
      success: function(res){
        var result = JSON.parse(res.data);
        if (result.status == 1){
          that.data.upload.push(result.data);
        }
      }
    })
  },
  chooseImage(e) {
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],  //可选择原图或压缩后的图片
      sourceType: ['album', 'camera'], //可选择性开放访问相册、相机
      success: res => {
        const images = this.data.images.concat(res.tempFilePaths)
        // 限制最多只能留下1张照片
        this.data.images = images.length <= 1 ? images : images.slice(0, 1)
        $digest(this)

        this.uploadImg();
      }
    })
  },
  removeImage(e) {
    const idx = e.target.dataset.idx
    this.data.images.splice(idx, 1)
    $digest(this)
  },
  handleImagePreview(e) {
    const idx = e.target.dataset.idx
    const images = this.data.images
    wx.previewImage({
      current: images[idx],  //当前预览的图片
      urls: images,  //所有要预览的图片
    })
  },

  bindContent: function(e){
    this.setData({
      content: e.detail.value
    });

  },
  subInfo: function(){
    if (this.data.content == ''){
      wx.showToast({
        title: '内容不能为空',
        icon: 'none',
        duration: 1500
      })
      return;
    }
    // if(this.data.upload.length == 0){
    //   wx.showToast({
    //     title: '请选择图片上传',
    //     icon: 'none',
    //     duration: 1500
    //   })
    //   return;
    // }
    if(this.data.select == 0){
      this.setData({
        selectData : "To_Be_Follow_up"
      })
    } else if (this.data.select == 1){
      this.setData({
        selectData : "Completed"
      })
    }else if(this.data.select == 2){
      this.setData({
        selectData : "Uninentional"
      })
    }else{
      this.setData({
        selectData : ""
      })
    }
    wx.navigateTo({
      url: '/pages/genjin2/genjin2?customer_id=' + this.data.id + "&desc=" + this.data.content + "&image=" + this.data.upload[0] + "&time=" + this.data.dateTimeArray[0][this.data.dateTime[0]] + "-" + this.data.dateTimeArray[1][this.data.dateTime[1]] + "-" + this.data.dateTimeArray[2][this.data.dateTime[2]] + " " + this.data.dateTimeArray[3][this.data.dateTime[3]] + ":" + this.data.dateTimeArray[4][this.data.dateTime[4]] + ":" + this.data.dateTimeArray[5][this.data.dateTime[5]] + "&follow_status=" + this.data.selectData,
    })
    // wx.request({
    //   url: app.globalData.httpurl +'v2/follow/create',
    //   header: {
    //     "Authorization": "bearer " + app.globalData.access_token
    //   },
    //   data:{
    //     "customer_id": this.data.id,
    //     "desc": this.data.content,
    //     "image": this.data.upload[0],
    //     "time": dateTimeArray[0][dateTime[0]] + "-" + dateTimeArray[1][dateTime[1]] + "-" + dateTimeArray[2][dateTime[2]] + " " + dateTimeArray[3][dateTime[3]] + ":" + dateTimeArray[4][dateTime[4]] + ":" +  dateTimeArray[5][dateTime[5]] ,
    //     "follow_status": this.data.selectData
    //   },
    //   method:"POST",
    //   success:function(res){
    //     if(res.data.status == 0){
    //       wx.navigateBack({
    //         delta: 1
    //       })
    //     }
    //   }
    // })
  },
  bindTimeChange:function(e){
    this.setData({
      time: e.detail.value
    })
  },
  bindselect:function(e){
    this.setData({
      select: e.currentTarget.dataset.id
    })
  },
  changeDate(e) {
    this.setData({ date: e.detail.value });
  },
  changeTime(e) {
    this.setData({ time: e.detail.value });
  },
  changeDateTime(e) {
    this.setData({ dateTime: e.detail.value });
  },
  changeDateTime1(e) {
    this.setData({ dateTime1: e.detail.value });
  },
  changeDateTimeColumn(e) {
    var arr = this.data.dateTime, dateArr = this.data.dateTimeArray;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      dateTimeArray: dateArr
    });
  },
  changeDateTimeColumn1(e) {
    var arr = this.data.dateTime1, dateArr = this.data.dateTimeArray1;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      dateTimeArray1: dateArr
    });
  }
})